package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.MoneyPercentage;
import com.ruoyi.system.domain.RetrievalRecord;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.service.IMoneyPercentageService;
import com.ruoyi.system.service.IRetrievalRecordService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RecordMoneys;
import com.ruoyi.system.service.IRecordMoneysService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 店铺提现记录Controller
 *
 * @author ruoyi
 * @date 2021-03-09
 */
@RestController
@RequestMapping("/system/moneys")
public class RecordMoneysController extends BaseController
{
    @Autowired
    private IRecordMoneysService recordMoneysService;
    @Autowired(required = false)
    private TokenService tokenService;
    @Autowired
    private IMoneyPercentageService moneyPercentageService;
    @Autowired
    private IRetrievalRecordService retrievalRecordService;
    /**
     * 查询店铺提现记录列表
     */
   // @PreAuthorize("@ss.hasPermi('system:moneys:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordMoneys recordMoneys)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        recordMoneys.setUserId(Integer.parseInt(userId.toString()));
        recordMoneys.setMoney_type(0+"");
        List<RecordMoneys> list = recordMoneysService.selectRecordMoneysList(recordMoneys);
        return getDataTable(list);
    }


    //@PreAuthorize("@ss.hasPermi('system:moneys:listAll')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(RecordMoneys recordMoneys)
    {
        startPage();
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Long userId = loginUser.getUser().getUserId();
//        recordMoneys.setUserId(Integer.parseInt(userId.toString()));
        List<RecordMoneys> list = recordMoneysService.selectListAll(recordMoneys);
        return getDataTable(list);
    }




    /**
     * 导出店铺提现记录列表
     */
   // @PreAuthorize("@ss.hasPermi('system:moneys:export')")
    @Log(title = "店铺提现记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecordMoneys recordMoneys)
    {
        List<RecordMoneys> list = recordMoneysService.selectRecordMoneysList(recordMoneys);
        ExcelUtil<RecordMoneys> util = new ExcelUtil<RecordMoneys>(RecordMoneys.class);
        return util.exportExcel(list, "moneys");
    }

    /**
     * 获取店铺提现记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:moneys:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(recordMoneysService.selectRecordMoneysById(id));
    }

    /**
     * 新增店铺提现记录
     */
    @PreAuthorize("@ss.hasPermi('system:moneys:add')")
    @Log(title = "店铺提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordMoneys recordMoneys)
    {
        return toAjax(recordMoneysService.insertRecordMoneys(recordMoneys));
    }

    /**
     * 修改店铺提现记录
     */
    //@PreAuthorize("@ss.hasPermi('system:moneys:edit')")
    @Log(title = "店铺提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordMoneys recordMoneys)
    {
        System.out.println("ssssssssssssssssssssssssssssssssssssssssss"+recordMoneys);

        MoneyPercentage moneyPercentage = moneyPercentageService.selectMoneyPercentageById(1);
        RetrievalRecord retrievalRecord = new RetrievalRecord();
        retrievalRecord.setShopsname(recordMoneys.getShopsname());
        retrievalRecord.setTixianMoney(recordMoneys.getTixiMoney());
        //retrievalRecord.setExtractMoney();
        //recordMoneys.getTixiMoney();
        retrievalRecordService.insertRetrievalRecord(retrievalRecord);

        return toAjax(recordMoneysService.updateRecordMoneys(recordMoneys));
    }

    /**
     * 删除店铺提现记录
     */
    @PreAuthorize("@ss.hasPermi('system:moneys:remove')")
    @Log(title = "店铺提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(recordMoneysService.deleteRecordMoneysByIds(ids));
    }
}
